package com.perfect.controller;

import com.perfect.base.BaseController;
import com.perfect.base.ResultInfo;
import com.perfect.query.RoleQuery;
import com.perfect.service.RoleService;
import com.perfect.vo.Role;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;


    /**
     * 进入角色管理主页
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "role/role";
    }

    /**
     * 多条件查询角色列表
     *
     * @param roleQuery
     * @return java.util.Map
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> userList(RoleQuery roleQuery) {
        return roleService.queryByParamsForTable(roleQuery);
    }

    /**
     * 添加角色
     *
     * @param role 角色记录
     * @return com.perfect.base.ResultInfo
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultInfo saveRole(Role role) {
        roleService.saveRole(role);
        return success("角色添加成功");
    }

    /**
     * 角色修改添加弹出框页面
     *
     * @return
     */
    @RequestMapping("/addOrUpdateRolePage")
    public String addOrUpdateRolePage(Integer id, Model model) {
        if (null != id) {
            model.addAttribute("role", roleService.selectByPrimaryKey(id));
        }
        return "role/add_update";
    }

    /**
     * 更新角色
     *
     * @param role
     * @return com.perfect.base.ResultInfo
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultInfo updateRole(Role role) {
        roleService.updateRole(role);
        return success("修改成功");
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/delRole")
    @ResponseBody
    public ResultInfo delRole(Integer roleId) {
        roleService.delRole(roleId);
        return success("删除成功");
    }

    /**
     * 角色授权弹出层
     *
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("/grant")
    public String grant(Integer roleId, Model model) {
        if (null != roleId) {
            model.addAttribute("roleId", roleId);
        }
        return "role/grant";
    }

    /**
     * 角色授权
     *
     * @param roleId 角色ID
     * @param mIds   资源ID
     * @return com.perfect.base.ResultInfo
     */
    @RequestMapping("/grantRole")
    @ResponseBody
    public ResultInfo grantRole(Integer roleId, Integer[] mIds) {
        roleService.grantRole(roleId, mIds);
        return success("角色授权成功！");
    }
}
